package com.bootdo.common.exception;


import org.apache.commons.lang3.StringUtils;

/**
 * 业务处理异常
 *
 * @author dudianbo
 * @since 2018.1.12
 */
public class BusinessException extends RuntimeException {

    private static final long serialVersionUID = 1;

    private ErrorCode errorCode;
    private String errorMsg;

    private Exception e;

    public BusinessException() {
        super();
    }

    public BusinessException(String errorMsg) {
        super(errorMsg);
    }

    public BusinessException(ErrorCode errorCode) {
        super(errorCode.getMessage());
        this.errorCode = errorCode;
    }

    public BusinessException(ErrorCode errorCode, String errorMsg) {
        super(errorCode.getMessage());
        this.errorCode = errorCode;
        this.errorMsg=errorMsg;
    }

    public BusinessException(ErrorCode errorCode, String errorMsg, Exception e) {
        super(errorMsg);
        this.errorCode = errorCode;
        this.e = e;
        this.errorMsg=errorMsg;
    }

    public ErrorCode getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(ErrorCode errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMsg() {
        String pattern = errorCode != null ? errorCode.getMessage() : "{0}";
        String arg = StringUtils.isBlank(errorMsg) ? "" : errorMsg;
        return pattern+  arg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }
}
